package com.example.java_controller;

import com.example.java_pojo.Brand;
import com.example.java_service.BrandService;
import com.example.java_util.utils.Code;
import com.example.java_util.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/brand")
@CrossOrigin
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping
    public Result selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Brand> brands = brandService.selectAll();
        Integer code = brands != null ? Code.GET_OK : Code.GET_ERR;
        String msg = (code.equals(20041)) ? "查询成功" : "查询失败";
        return new Result(code, brands, msg);
    }

    @PostMapping
    public Result add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String brand_name = request.getParameter("brand_name");
        String company_name = request.getParameter("company_name");
        Integer ordered = Integer.parseInt(request.getParameter("ordered"));
        String description = request.getParameter("description");
        Integer status = Integer.parseInt(request.getParameter("status"));
        Brand brand = new Brand();
        brand.setBrand_name(brand_name);
        brand.setCompany_name(company_name);
        brand.setOrdered(ordered);
        brand.setDescription(description);
        brand.setStatus(status);
        System.out.println(brand);
        boolean flag = brandService.add(brand);
        Integer code = flag ? Code.SAVE_OK : Code.SAVE_ERR;
        String msg = flag ? "添加成功" : "添加失败";
        return new Result(code, brand, msg);
    }

    @PutMapping
    public Result update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        String brand_name = request.getParameter("brand_name");
        String company_name = request.getParameter("company_name");
        Integer ordered = Integer.parseInt(request.getParameter("ordered"));
        String description = request.getParameter("description");
        Integer status = Integer.parseInt(request.getParameter("status"));
        Brand brand = new Brand();
        brand.setId(id);
        brand.setBrand_name(brand_name);
        brand.setCompany_name(company_name);
        brand.setOrdered(ordered);
        brand.setDescription(description);
        brand.setStatus(status);
        System.out.println(brand);
        boolean flag = brandService.update(brand);
        Integer code = flag ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String msg = flag ? "修改成功" : "修改失败";
        return new Result(code, brand, msg);
    }

    @DeleteMapping
    public Result delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        boolean flag = brandService.delete(id);
        Integer code = flag ? Code.DELETE_OK : Code.DELETE_ERR;
        String msg = flag ? "删除成功" : "删除失败";
        return new Result(code, msg);
    }

    @PostMapping("/selectBrand")
    public Result selectBrand(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String company_name = request.getParameter("company_name");
        String brand_name = request.getParameter("brand_name");
        List<Brand> list = brandService.selectBrand(company_name, brand_name);
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "查询成功" : "查询失败";
        return new Result(code, list, msg);
    }

    @PostMapping("/deleteByIds")
    public Result deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] ids = request.getParameterValues("selectedIds");
        String[] value=ids[0].split(",");
        Integer[] arr = new Integer[value.length];
        for (int i = 0; i < value.length; i++) {
            System.out.println(value[i]);
            arr[i] = Integer.parseInt(value[i]);
        }
        System.out.println(arr);
        boolean flag = brandService.deleteByIds(arr);
        Integer code = flag ? Code.DELETE_OK : Code.DELETE_ERR;
        String msg = flag ? "删除成功" : "删除失败";
        return new Result(code, msg);
    }
}
